package com.aldobucchi.alias
{
	import com.aldobucchi.ds.container.zet.IRWSet;
	import com.aldobucchi.ds.container.zet.impl.owSet;
	import com.aldobucchi.utils.AbstractTest;
	
	import flash.events.Event;

	public class AliasTest extends AbstractTest
	{




		public function testMisc( ):void
		{
		
			
			var ctx:AliasContext = new AliasContext( nativeCompare );
			
			var ra:AliasRecord = ctx.getRecord( "a" );
			var rb:AliasRecord = ctx.getRecord( "b" );					
			var rc:AliasRecord = ctx.getRecord( "c" );					
		
			ra.addEventListener( Event.CHANGE, onRecordChange );
			rb.addEventListener( Event.CHANGE, onRecordChange );
			rc.addEventListener( Event.CHANGE, onRecordChange );
		

			// initial state
			
			assertTrue( ra.aliases.compare( owSet( ["a"] ) ).isEqual  );
			assertTrue( rb.aliases.compare( owSet( ["b"] ) ).isEqual  );
		
			assertEquals( ra.cannonical, "a" );
			assertEquals( rb.cannonical, "b" );
			
		
			assertEquals( changedRecords.size, 0 );
		
			ctx.eq( "a", "b" );
			
				// were change events dispatched correctly??
				// only a should have changed
				assertEquals( changedRecords.size, 1 );
				assertTrue( changedRecords.contains( ra ) );
				changedRecords.clear( );			
			
			
			assertEquals( ra.cannonical, rb.cannonical );
			assertEquals( ra.aliases.size, 2 );
			assertTrue( ra.aliases.compare( rb.aliases ).isEqual );
		
			assertEquals( ra.cannonical, "b" );
			
			ctx.eq( "a", "c" );
			
				// were change events dispatched correctly??
				// a and b should have changed... c remains pointing to self
				assertEquals( changedRecords.size, 2 );
				assertTrue( changedRecords.contains( ra ) );
				assertTrue( changedRecords.contains( rb ) );
				changedRecords.clear( );
			
			assertEquals( ra.cannonical, "c" );
			assertEquals( ra.cannonical, rb.cannonical );
			assertEquals( ra.cannonical, rc.cannonical );
			
			assertEquals( rc.aliases.size, 3 );
			
			ctx.neq( "b", "a" );

				// were change events dispatched correctly??
				// only b changed... it now points to self
				assertEquals( changedRecords.size, 1 );
				assertTrue( changedRecords.contains( rb ) );

			
			assertEquals( rb.aliases.size, 1 );
			assertEquals( rb.cannonical, "b" );
			
			assertEquals( ra.aliases.size, 2 );
			assertEquals( ra.cannonical , "c" );
			
			assertEquals( rc.cannonical , "c" );
			
		
		}

		
		private var changedRecords:IRWSet = owSet( );


		private function onRecordChange( event:Event ):void
		{
			changedRecords.add( event.target );		
		
		}





		private function nativeCompare( a:*, b:* ):int
		{
			if ( a > b )
				return 1;
			
			if ( a < b )
				return -1;
			
			return 0;
		}





	}

}